package com.dbgs.insurance.insuredperson.view.wheeldata;

import android.app.Activity;
import android.content.res.AssetManager;

import com.dbgs.insurance.insuredperson.utils.XmlParserHandler;
import com.dbgs.insurance.insuredperson.view.city.bean.CityModel;
import com.dbgs.insurance.insuredperson.view.city.bean.DistrictModel;
import com.dbgs.insurance.insuredperson.view.city.bean.ProvinceModel;
import com.hss.base.util.LogUtils;
import com.lz.commonlibrary.utils.ListUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


public class WheelViewActivity extends Activity {

    /**
     * ����ʡ
     */
    protected String[] mProvinceDatas;
    /**
     * key - ʡ value - ��
     */
    protected Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
    /**
     * key - �� values - ��
     */
    protected Map<String, String[]> mDistrictDatasMap = new HashMap<String, String[]>();

    /**
     * key - �� values - �ʱ�
     */
    protected Map<String, String> mZipcodeDatasMap = new HashMap<String, String>();

    /**
     * ��ǰʡ�����
     */
    protected String mCurrentProviceName;
    /**
     * ��ǰ�е����
     */
    protected String mCurrentCityName;
    /**
     * ��ǰ������
     */
    protected String mCurrentDistrictName = "";

    /**
     * ��ǰ�����������
     */
    protected String mCurrentZipCode = "";

    /**
     * ����ʡ�����XML���
     */

    protected void initProvinceDatas() {
        List<ProvinceModel> provinceList = null;
        AssetManager asset = getAssets();
        try {
            InputStream input = asset.open("province_data.xml");
            // ����һ������xml�Ĺ�������
//            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//            dbf.setExpandEntityReferences(false);


            SAXParserFactory spf = SAXParserFactory.newInstance();
            //解析器在解析时验证 XML 内容。
//            spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

            spf.setFeature("http://xml.org/sax/features/external-general-entities", false);
            spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
//            spf.setXIncludeAware(false);
//            spf.setValidating(true);
            // ����xml
            SAXParser parser = spf.newSAXParser();
            XmlParserHandler handler = new XmlParserHandler();
            parser.parse(input, handler);
            input.close();
            // ��ȡ�������������
            provinceList = handler.getDataList();
            //*/ ��ʼ��Ĭ��ѡ�е�ʡ���С���
            if (!ListUtils.isEmpty(provinceList)) {
                mCurrentProviceName = provinceList.get(0).getName();
                List<CityModel> cityList = provinceList.get(0).getCityList();
                if (!ListUtils.isEmpty(cityList)) {
                    mCurrentCityName = cityList.get(0).getName();
                    List<DistrictModel> districtList = cityList.get(0).getDistrictList();
                    mCurrentDistrictName = districtList.get(0).getName();
                    mCurrentZipCode = districtList.get(0).getZipcode();
                }
            }
            mProvinceDatas = new String[provinceList.size()];
            for (int i = 0; i < provinceList.size(); i++) {
                // ��������ʡ�����
                mProvinceDatas[i] = provinceList.get(i).getName();
                List<CityModel> cityList = provinceList.get(i).getCityList();
                String[] cityNames = new String[cityList.size()];
                for (int j = 0; j < cityList.size(); j++) {
                    // ����ʡ����������е����
                    cityNames[j] = cityList.get(j).getName();
                    List<DistrictModel> districtList = cityList.get(j).getDistrictList();
                    String[] distrinctNameArray = new String[districtList.size()];
                    DistrictModel[] distrinctArray = new DistrictModel[districtList.size()];
                    for (int k = 0; k < districtList.size(); k++) {
                        // ����������������/�ص����
                        DistrictModel districtModel = new DistrictModel(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        // ��/�ض��ڵ��ʱ࣬���浽mZipcodeDatasMap
                        mZipcodeDatasMap.put(districtList.get(k).getName(), districtList.get(k).getZipcode());
                        distrinctArray[k] = districtModel;
                        distrinctNameArray[k] = districtModel.getName();
                    }
                    // ��-��/�ص���ݣ����浽mDistrictDatasMap
                    mDistrictDatasMap.put(cityNames[j], distrinctNameArray);
                }
                // ʡ-�е���ݣ����浽mCitisDatasMap
                mCitisDatasMap.put(provinceList.get(i).getName(), cityNames);
            }
        } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
        } finally {

        }
    }

}
